import traceback

try:
    # 这里是可能抛出异常的代码
    x = 1
    y = 0
    result = x / y
except Exception as e:
    # 获取异常堆栈信息
    exc_info = traceback.format_exc()
    # 添加堆栈信息到异常信息中
    error_message = f"An error occurred: {e}. Stack trace: {exc_info}"
    print(error_message)